package com.md.api.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.md.api.common.utils.ImageUtils;
import com.md.api.common.utils.StringUtils;
import com.md.api.common.utils.TiandituUtil;
import com.md.api.common.utils.TokenUtil;
import com.md.api.entity.HttpResult;
import com.md.api.entity.SysConfig;
import com.md.api.entity.dto.ComposeQueryDto;
import com.md.api.service.SysUserSevice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RequestMapping("/common")
@RestController
public class CommonController {


	@Resource
	SysUserSevice sysUserSevice;

	@Resource
	TokenUtil tokenUtil;



	@RequestMapping("/shopLocation")
	public HttpResult shopLocation() {
		return sysUserSevice.shopLocation();
	}


	@RequestMapping("/addressTip")
	public HttpResult addressTip(@RequestBody ComposeQueryDto queryDto) {
		if (StringUtils.isEmpty(queryDto.getKeyword())||StringUtils.isEmpty(queryDto.getAdcode())){
			return HttpResult.PARAM_ERROR;
		}
		if (queryDto.getAdcode().length()<7){
			queryDto.setAdcode("156"+queryDto.getAdcode());
		}
		JSONArray addressTip = TiandituUtil.getAddressTip(queryDto.getKeyword(),queryDto.getAdcode());
		return new HttpResult(addressTip);
	}



	@RequestMapping("/getConfig")
	public HttpResult getConfig(@RequestBody SysConfig config) {
		if (StringUtils.isEmpty(config.getConfigKey())){
			return HttpResult.PARAM_ERROR;
		}
		return sysUserSevice.getConfig(config);
	}


	@RequestMapping("/shareQrCode")
	public HttpResult shareQrCode(HttpServletRequest request) {
		String userId = tokenUtil.getUserId(request);
		String qrCodePath = ImageUtils.smallProgCode("pages/index/index?parentUserId="+userId,userId);
		return new HttpResult(qrCodePath);
	}

}
